package com.zj.syzs.utils;

/**
 * @author: 张唯
 * @�?�?  �?
 * @version 1.0
 * @创建时间�?014-8-5 下午5:58:49
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {
    /**
     * sd���ĸ�Ŀ¼
     */
    private static String mSdRootPath = Environment.getExternalStorageDirectory().getPath();
    /**
     * �ֻ�Ļ����Ŀ¼
     */
    private static String mDataRootPath = null;
    /**
     * ����Image��Ŀ¼��
     */
    private final static String FOLDER_NAME = "/tea/image";

    public FileUtils(Context context) {
        mDataRootPath = context.getCacheDir().getPath();
    }

    /**
     * ��ȡ����Image��Ŀ¼
     *
     * @return
     */
    private String getStorageDirectory() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? mSdRootPath + FOLDER_NAME : mDataRootPath + FOLDER_NAME;
    }

    /**
     * ����Image�ķ�������sd���洢��sd����û�оʹ洢���ֻ�Ŀ¼
     *
     * @param fileName
     * @param bitmap
     * @throws IOException
     */
    public void savaBitmap(String url, Bitmap bitmap) throws IOException {
        if (bitmap == null) {
            return;
        }
        String path = getStorageDirectory();
        File folderFile = new File(path);
        if (!folderFile.exists()) {
            folderFile.mkdirs();
        }
        File file = new File(path + File.separator + getFileName(url));
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    }

    /**
     * ���ֻ����sd����ȡBitmap
     *
     * @param fileName
     * @return
     */
    public Bitmap getBitmap(String url) {
        return BitmapFactory.decodeFile(getStorageDirectory() + File.separator + getFileName(url));
    }

    /**
     * �ж��ļ��Ƿ����
     *
     * @param fileName
     * @return
     */
    public boolean isFileExists(String fileName) {
        return new File(getStorageDirectory() + File.separator + getFileName(fileName)).exists();
    }

    /**
     * ��ȡ�ļ��Ĵ�С
     *
     * @param fileName
     * @return
     */
    public long getFileSize(String url) {
        return new File(getStorageDirectory() + File.separator + getFileName(url)).length();
    }

    /**
     * ɾ��SD�������ֻ�Ļ���ͼƬ��Ŀ¼
     */
    public void deleteFile() {
        File dirFile = new File(getStorageDirectory());
        if (!dirFile.exists()) {
            return;
        }
        if (dirFile.isDirectory()) {
            String[] children = dirFile.list();
            for (int i = 0; i < children.length; i++) {
                new File(dirFile, children[i]).delete();
            }
        }

        dirFile.delete();
    }

    /**
     * @return String ��������
     * @throws
     * @Title: getFileName
     * @˵ ��: ���url��ȡ�ļ���
     * @�� ��: @param url
     * @�� ��: @return
     */
    public String getFileName(String url) {
        return url.substring(url.lastIndexOf("/") + 1);
    }

}